﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace WinFormsTaskManager
{
    // 任务管理类，处理所有任务逻辑
    public class TaskManager
    {
        private List<Task> _tasks;

        public TaskManager()
        {
            _tasks = new List<Task>();
        }

        // 添加任务
        public void AddTask(Task task)
        {
            _tasks.Add(task);
        }

        // 获取所有任务
        public List<Task> GetAllTasks()
        {
            return new List<Task>(_tasks);
        }

        // 标记任务为已完成
        public bool MarkTaskAsCompleted(int taskId)
        {
            var task = _tasks.FirstOrDefault(t => t.Id == taskId);
            if (task != null)
            {
                task.IsCompleted = true;
                return true;
            }
            return false;
        }

        // 搜索任务
        public List<Task> SearchTasks(string keyword)
        {
            if (string.IsNullOrWhiteSpace(keyword))
                return new List<Task>(_tasks);

            return _tasks.Where(t =>
                t.Title.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0 ||
                t.Description.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0
            ).ToList();
        }

        // 保存任务到文件
        public bool SaveTasks(string filePath)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Task>));
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    serializer.Serialize(writer, _tasks);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        // 从文件加载任务
        public bool LoadTasks(string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Task>));
                    using (StreamReader reader = new StreamReader(filePath))
                    {
                        _tasks = (List<Task>)serializer.Deserialize(reader);

                        if (_tasks.Count > 0)
                        {
                            int maxId = _tasks.Max(t => t.Id);
                            Task.ResetNextId(maxId);
                        }
                    }
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        // 删除任务
        public bool DeleteTask(int taskId)
        {
            var task = _tasks.FirstOrDefault(t => t.Id == taskId);
            if (task != null)
            {
                _tasks.Remove(task);
                return true;
            }
            return false;
        }

        // 更新任务
        public bool UpdateTask(Task updatedTask)
        {
            var existingTask = _tasks.FirstOrDefault(t => t.Id == updatedTask.Id);
            if (existingTask != null)
            {
                existingTask.Title = updatedTask.Title;
                existingTask.Description = updatedTask.Description;
                existingTask.DueDate = updatedTask.DueDate;
                existingTask.Priority = updatedTask.Priority;
                existingTask.IsCompleted = updatedTask.IsCompleted;
                return true;
            }
            return false;
        }
    }
}
